using System;
using System.Collections.Generic;
using System.IO;

namespace ParasiTracer
{
    internal struct FuncItem
    {
        public string   name;
        public bool     enabled;
        public bool     enableChildren;
        public int      paramsOpt;
    }

    internal sealed class FuncTable
    {
        static FuncTable _inst = new FuncTable();
        static FuncTable () { }
        public static FuncTable inst { get { return _inst; } }

        public readonly FuncItem [] funcs = null;

        FuncTable()
        {
            #if DEBUG_FILE
            using (StreamWriter sw = File.AppendText(@"\Storage card\debug.txt")) {
                sw.WriteLine("FuncTable");
                sw.Close();
            }
            #endif

            var items       = new List<FuncItem>();
            var currentDir  = SafeDirectory.inst.getCurrent();
            var path        = 
                currentDir + 
                Path.DirectorySeparatorChar + 
                "ftable.txt"; 

            #if DEBUG_FILE
            using (StreamWriter sw = File.AppendText(@"\Storage card\debug.txt")) {
                sw.WriteLine(path);
                sw.Close();
            }
            #endif

            if (!File.Exists(path))
                Logger.inst.logError("No existe el archivo {0}", path);

            using (StreamReader sr = new StreamReader(path))
            {
                string line;

                while (null != (line = sr.ReadLine()))
                {
                    string [] lineParts = line.Split(' ');

                    if ((lineParts == null) || (lineParts.Length != 4))
                        continue;

                    FuncItem fi         = new FuncItem();
                    fi.name             = lineParts[1];
                    fi.enabled          = "1" == lineParts[2];
                    fi.enableChildren   = "1" == lineParts[3];
                    items.Add (fi);
                }

                funcs = items.ToArray();
                sr.Close();
            }
        }
    }
}
